Skip to content
This repository has been archived by the owner on Sep 30, 2023. It is now read-only.

Added applyEntry function to Index. #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

CSDUMMI
Copy link

@CSDUMMI CSDUMMI commented Oct 3, 2021

I implemented this function because I want it to be easier to customize validation
of entries without modifying or knowing about the internal data structure used
in the KVStore.

If this PR is merged I plan on implementing similar functions on the DocStore, FeedStore and EventLog.

I think it is important for OrbitDB to be easily customizable and this feature should
lower the effort for customizing validation significantly.

In it's current form, the applyEntry function is not a breaking change, but an added
pattern that can be used to ease the customization efforts of Stores.

If this pattern is implemented I also feel ready to finally finish Part 5 of the Field Manual by introducing this pattern and implementing moderation rules using it.

Thanks for your consideration!

Custom Validation of Entries on the KeyValueIndex

To create a custom validation of the Index using the applyEntry function:

const KeyValueIndex = require("orbit-db-kvstore/src/KeyValueIndex")

// An index that only allows a key to be added iff it hasn't been added before.
class Index extends KeyValueIndex {
      applyEntry(entry) {
           if(entry.payload.op === "PUT" && this._index[entry.payload.key] === undefined) {
                super().applyEntry(entry) // or: this._index[entry.payload.key] = entry.payload.value
           }
      }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant